import { FileService } from '@app/file';
import { Controller, Get, Param, Res } from '@nestjs/common';
import { existsSync } from 'fs';
import { Response } from 'express';

@Controller('media')
export class MediaController {
    constructor(
        private readonly fileService: FileService
    ) { }

    @Get("shop/:hash_key/avatar")
    getShopAvatar(
        @Param("hash_key") hash_key: string,
        @Res() res: Response
    ) {
        const path = this.fileService.getShopAvatar(hash_key);
        if (existsSync(path)) {
            res.sendFile(path);
        } else {
            res.sendStatus(404)
        }
    }

    @Get("goods/:hash_key/avatar")
    getGoodsAvatar(
        @Param("hash_key") hash_key: string,
        @Res() res: Response
    ) {
        const path = this.fileService.getGoodsAvatar(hash_key);
        if (existsSync(path)) {
            res.sendFile(path);
        } else {
            res.sendStatus(404)
        }
    }

}
